<template>
    <div class="all">
        <div class="bread">首页>>车辆管理>>司机记录</div>
        <div class="nav">
            <div>
                <el-button   type="primary"  plain @click="dialogVisible = true">添加司机记录</el-button> 
                <span>发布时间</span>
                <span>车型：    
                     <el-select v-model="value" placeholder="选择车型" style="width: 240px">
                       <el-option
                         v-for="item in options"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value"
                       />
                     </el-select>
                </span>
                <span>关键字<input></span>
                <span>搜索</span>
            </div>
        </div>
        <div class="content">
          <el-table v-loading="tableloading"  :data="tableData" border size="small" :row-style="{height: '35px'}" :cell-style="{padding: '0'}" style="width: 100% ;height:calc(100% - 150px);" >
            <el-table-column fixed label="序号" type="index" width="60" />
            <!-- <el-table-column fixed prop="xuhao" label="序号" width="60"/> -->
              <!-- <el-table-column prop="dingdanhao" label="所属订单号" width="120" /> -->
              <el-table-column prop="sijixingming" label="司机姓名" width="120" />
              <el-table-column prop="chushengriqi" label="出生日期" width="120" />
              <el-table-column prop="jiashinianning" label="驾驶年龄" width="120" />
              <el-table-column prop="jiazheng" label="驾证" width="120" />
              <el-table-column prop="shenfenzheng" label="身份证" width="120" />
              <el-table-column prop="tijian" label="体检报告" width="120" />
              <el-table-column prop="beizhu" label="备注" width="160" />

              <el-table-column fixed="right" label="操作" width="120">
                <template #default="scope">
                  <el-button link type="primary" size="small" @click.prevent="bianjiClick(scope.$index)"
                    >编辑</el-button>
                  <el-button link type="primary" size="small" @click.prevent="deleteInfo(scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <Page :message="total" @get-message="getMessage"></Page>
        </div>
    </div>

    <!-- 添加记录对话框 -->
     <el-dialog
         v-model="dialogVisible"
         title="添加司机记录"
         width="500"
         :before-close="handleClose"
         style="text-align:center;padding-top: 10px;"
       >
       <!-- 添加记录的表单内容 -->
      <el-form :model="form" label-width="auto" style="max-width: 800px;padding: 0px 40px 40px 40px;">     
          <el-form-item label="司机姓名">
            <el-input v-model="form.sijixingming" />
          </el-form-item>

         <el-form-item label="出生日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form.chushengriqi"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 
          <el-form-item label="驾驶年龄">
            <el-input v-model="form.jiashinianning" />
          </el-form-item>

          <el-form-item label="驾证">
            <el-input v-model="form.jiazheng" />
          </el-form-item>

          <el-form-item label="身份证">
            <el-input v-model="form.shenfenzheng" />
          </el-form-item>

          <el-form-item label="体检">
            <el-input v-model="form.tijian" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form.beizhu" />
          </el-form-item>


        </el-form>

         <template #footer>
           <div class="dialog-footer">
             <el-button @click="dialogVisible = false">取消</el-button>
             <el-button type="primary" @click="addInfo()">
               确认
             </el-button>
           </div>
         </template>
     </el-dialog>
       

    <!-- 编辑记录对话框 -->
     <el-dialog
         v-model="bianjiVisible"
         title="编辑司机记录"
         width="500"
         :before-close="handleClose"
         style="text-align:center;padding-top: 10px;"
       >
       <!-- 编辑记录的表单内容 -->
      <el-form :model="form1" label-width="auto" style="max-width: 800px;padding: 0px 40px 40px 40px;">     
          <el-form-item label="司机姓名">
            <el-input v-model="form1.sijixingming" />
          </el-form-item>

         <el-form-item label="出生日期">
            <el-col :span="11">
              <el-date-picker
                v-model="form1.chushengriqi"
                type="date"
                placeholder="选择时间"
                style="width: 100%"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-col>
          </el-form-item> 
          <el-form-item label="驾驶年龄">
            <el-input v-model="form1.jiashinianning" />
          </el-form-item>

          <el-form-item label="驾证">
            <el-input v-model="form1.jiazheng" />
          </el-form-item>

          <el-form-item label="身份证">
            <el-input v-model="form1.shenfenzheng" />
          </el-form-item>

          <el-form-item label="体检">
            <el-input v-model="form1.tijian" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="form1.beizhu" />
          </el-form-item>


        </el-form>


         <template #footer>
           <div class="dialog-footer">
             <el-button @click="bianjiVisible = false">取消</el-button>
             <el-button type="primary" @click="putInfo()">
               确认
             </el-button>
           </div>
         </template>
     </el-dialog>       
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {addSiji,getSiji,deleteSiji,putSiji} from '../../api/siji.js'
// 添加翻页组件
import Page from '../../zujian/page.vue'

//加载
const tableloading = ref(false)

//列表总数
const total = ref()
//页数
const count = ref(0)
//翻页组件页数变化
const getMessage = (msg)=>{
  console.log(msg)
  count.value = msg - 1
  getInfo()
}

//添加记录对话框开关
const dialogVisible = ref(false)
//编辑记录对话框开关
const bianjiVisible = ref(false)


//车型选择
const options = [
    {
        value: '防撞车',
        label: '防撞车',
    },
    {
        value: '高空作业车',
        label: '高空作业车',
    },
    {
        value: '桥梁检测车',
        label: '桥梁检测车',
    },
    {
        value: '自行走',
        label: '自行走',
    }
]
//表格 标题和内容
// const tableData = [
//     {
//         xuhao: '01',
//         sijixingming: '司机a',
//         chushengriqi: '2000-06-11',
//         jiashinianning: '6年',
//         jiazheng: '',
//         shenfenzheng: '',
//         tijian: '',
//         beizhu: '司机信息',
//     }
// ]
const tableData = ref()
// 添加记录的表单内容
const form = ref({
        xuhao: '',
        sijixingming: '',
        chushengriqi: '',
        jiashinianning: '',
        jiazheng: '',
        shenfenzheng: '',
        tijian: '',
        beizhu: '',
})

// 添加记录的表单内容
const form1 = ref({
        xuhao: '01',
        sijixingming: '司机a',
        chushengriqi: '2000-06-11',
        jiashinianning: '6年',
        jiazheng: '',
        shenfenzheng: '',
        tijian: '',
        beizhu: '司机信息',
})

// 清空新增表单
const clear = ()=>{
  form.value = {
        xuhao: '',
        sijixingming: '',
        chushengriqi: '',
        jiashinianning: '',
        jiazheng: '',
        shenfenzheng: '',
        tijian: '',
        beizhu: '',
  }
}

// 编辑车辆记录按钮
const bianjiClick = (id) => {
    bianjiVisible.value = true
    form1.value = tableData.value[id]
    console.log(data)
    console.log('bianji')
}

// 获取司机记录
const getInfo = ()=>{
  tableloading.value = true
  getSiji(count.value).then(res =>{
    tableloading.value = false
    console.log(res)
    tableData.value = res.data.content
    total.value = res.data.totalElements
  })
}

//新增司机记录
const addInfo = ()=>{
  addSiji(form.value).then(res=>{
    console.log(res)
    dialogVisible.value = false
    clear()
    getInfo()
  })
}

//删除司机记录
const deleteInfo = (id)=>{
  const idx = tableData.value[id].xuhao
  deleteSiji(idx).then(res=>{
    console.log(res)
    getInfo()
  })
}

//修改司机记录
const putInfo = ()=>{
  putSiji(form1.value).then(res =>{
    console.log(res)
    bianjiVisible.value = false
    getInfo()
  })
}

onMounted(()=>{
  getInfo()
})

</script>

<style lang="scss" scoped>
.all {
    background-color: #d6d6d6;
    height: 100%;
    overflow: hidden;
    padding-left: 10px;

    .bread {
        height: 30px;
        background-color: #fff;
        margin-bottom: 10px;
    }

    .nav {
        height: 40px;
        background-color: #fff;
        margin-bottom: 10px;
        padding: 5px;

        div {
            span {
                margin: 10px;
                line-height: 40px;
            }
        }
    }

    .content {
        background: #fff;
        height: 100%;
        padding: 5px;
        overflow: hidden;
    }
}
</style>